<template>
  <div class="app-container">
    <el-card style="width: 600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="客户 ID" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户 ID" disabled />
        </el-form-item>
        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户姓名" disabled />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInfo">
          <el-input style="width: 180px" v-model="form.contactInfo" placeholder="请输入联系方式" />
          <el-button style="margin-left: 5px" type="primary" @click="queryCustomerInfoByPhone(form.contactInfo)">查找</el-button>
        </el-form-item>
        <el-form-item label="预约服务类型" prop="appointmentServiceType">
          <el-select v-model="form.appointmentServiceType" placeholder="请选择预约服务类型">
            <el-option
              v-for="dict in dict.type.car_4s_appointment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预约时间" prop="appointmentTime">
          <el-date-picker clearable
                          v-model="form.appointmentTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约技师 ID" prop="technicianId">
          <el-input v-model="form.technicianId" placeholder="后台自动选择" disabled/>
        </el-form-item>
        <el-form-item label="预约技师名称" prop="technicianName">
          <el-input v-model="form.technicianName" placeholder="后台自动选择"  disabled/>
        </el-form-item>
        <el-form-item label="信息标记" prop="informationMark">
          <el-input v-model="form.informationMark" placeholder="备注:无" />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { listCar_4s_appointment_info, getCar_4s_appointment_info, delCar_4s_appointment_info, addCar_4s_appointment_info, updateCar_4s_appointment_info } from "@/api/car4s_api/car_4s_appointment_info";
import { getCar_4s_customer_info_ByPhone} from "@/api/car4s_api/car_4s_customer_info";
import {isNumeric} from "@/api/commen/common";
export default {
  name: "Car_4s_appointment_info",
  dicts: ['car_4s_appointment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约信息表格数据
      car_4s_appointment_infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        name: null,
        contactInfo: null,
        appointmentServiceType: null,
        appointmentTime: null,
        technicianId: null,
        technicianName: null,
        informationMark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contactInfo: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        appointmentServiceType: [
          { required: true, message: "预约服务类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预约信息列表 */
    getList() {
      this.loading = true;
      listCar_4s_appointment_info(this.queryParams).then(response => {
        this.car_4s_appointment_infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        appointmentInfoId: null,
        customerId: null,
        name: null,
        contactInfo: null,
        appointmentServiceType: null,
        appointmentTime: null,
        technicianId: null,
        technicianName: null,
        informationMark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.appointmentInfoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const appointmentInfoId = row.appointmentInfoId || this.ids
      getCar_4s_appointment_info(appointmentInfoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.appointmentInfoId != null) {
            updateCar_4s_appointment_info(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCar_4s_appointment_info(this.form).then(response => {
              this.$modal.msgSuccess("预约成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const appointmentInfoIds = row.appointmentInfoId || this.ids;
      this.$modal.confirm('是否确认删除预约信息编号为"' + appointmentInfoIds + '"的数据项？').then(function() {
        return delCar_4s_appointment_info(appointmentInfoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/car_4s_appointment_info/export', {
        ...this.queryParams
      }, `car_4s_appointment_info_${new Date().getTime()}.xlsx`)
    },
    //通过手机号查找客户的id与姓名进行回填
    queryCustomerInfoByPhone(phoneNum){
      if(phoneNum == null || phoneNum==''){
        this.$modal.msgWarning("请先输入手机号")
      }else if(phoneNum.length!=11){
        this.$modal.msgError("请检查手机号是否完整")
      }else if(!isNumeric(phoneNum)){
        this.$modal.msgError("请检查手机号格式是否正确")
      }
      else {
        getCar_4s_customer_info_ByPhone(phoneNum).then((res)=>{
          console.log('结果byPhone',res)
          if(res.data.contactInfo==null){
            this.$modal.msgError("不存在此手机号绑定的信息")
          }else {
            this.form.customerId = res.data.customerId
            this.form.name = res.data.name
            this.$modal.msgSuccess("查找成功")
          }
        })
      }

    }
  }
};
</script>
